// 实现这个项目的构建任务
const { src, dest, series, parallel, watch } = require('gulp')
const sass = require('gulp-sass')(require('sass'))
const loadPlugins = require('gulp-load-plugins')

const browserSync = require('browser-sync')
const del = require('del')

// 模板数据
const data = {
  menus: [
    {
      name: 'Home',
      icon: 'aperture',
      link: 'index.html'
    },
    {
      name: 'Features',
      link: 'features.html'
    },
    {
      name: 'About',
      link: 'about.html'
    },
    {
      name: 'Contact',
      link: '#',
      children: [
        {
          name: 'Twitter',
          link: 'https://twitter.com/w_zce'
        },
        {
          name: 'About',
          link: 'https://weibo.com/zceme'
        },
        {
          name: 'divider'
        },
        {
          name: 'About',
          link: 'https://github.com/zce'
        }
      ]
    }
  ],
  pkg: require('./package.json'),
  date: new Date()
}

// 自动载入已安装的插件
const plugins = loadPlugins()

// 文件清除任务
const clean = () => {
  return del(['dist', 'temp'])
}

// 样式编译任务
const style = () => {
  return src('src/assets/styles/*.scss', { base: 'src' })
    .pipe(sass())
    .pipe(dest('temp'))
}

// 脚本编译任务
const js = () => {
  return src('src/assets/scripts/*.js', { base: 'src' })
    .pipe(plugins.babel({ presets: ['@babel/preset-env'] }))
    .pipe(dest('temp'))
}

// 页面编译任务
const html = () => {
  return src('src/*.html', { base: 'src' })
    .pipe(plugins.swig({
      data
    }))
    .pipe(dest('temp'))
}

// 图片编译任务
const image = () => {
  return src('src/assets/images/**', { base: 'src' })
    .pipe(plugins.imagemin())
    .pipe(dest('temp'))
}
// 字体编译任务
const font = () => {
  return src('src/assets/fonts/**', { base: 'src' })
    .pipe(plugins.imagemin())
    .pipe(dest('temp'))
}

// 其他文件任务
const extra = () => {
  return src('public/**', { base: 'public' })
    .pipe(dest('temp'))
}

// 处理dist文件下 依赖的文件
const useref = () => {
  return src('temp/*.html', { base: 'temp' })
    .pipe(plugins.useref({ searchPath: ['temp', '.'] }))
    .pipe(plugins.if(/\.js$/, plugins.uglify())) // 压缩生成的js
    .pipe(plugins.if(/\.css$/, plugins.cleanCss())) // 压缩生成的css
    .pipe(plugins.if(/\.html$/, plugins.htmlmin({ collapseWhitespace: true, minifyCSS: true, minifyJS: true }))) // 压缩生成的html
    .pipe(dest('dist'))
}

const bs = browserSync.create()

const serve = () => {
  // 热加载
  // 监听文件变化，实时构建生成对应文件到 dist 目录下
  watch('src/assets/styles/*.scss', style)
  watch('src/assets/scripts/*.js', js)
  watch('src/*.html', html)

  // 静态资源 在开发过程中不参与构建 原因：构建前后效果在开发阶段无法体现，无需浪费构建资源
  watch([
    'src/assets/images/**',
    'src/assets/fonts/**',
    'public/**'
  ], browserSync.reload)

  bs.init({
    // 需要即时更新的 监听变更文件路径
    // 或者在文件构建完成时进行一次 reload
    files: 'temp',
    server: {
      baseDir: ['temp', 'src', 'public'],
      routes: {
        // 路由代理
        '/node_modules': 'node_modules'
      }
    }
  })
}

const dev = series(compile, serve)

const compile = series(clean, parallel(style, js, html))

const build = series(clean, parallel(series(compile, useref), image, font, extra))

module.exports = {
  style,
  js,
  html,
  image,
  dev,
  build
}